<?php
	$data = array();
	$data['title'] = "Liên hệ";
	$db = new model_mdel;
	if(isset($_POST['type'])){
		$name	= $_POST['con_name'];
		$email	= $_POST['con_email'];
		$phone	= $_POST['con_phone'];
		$message = $_POST['con_message'];
		$rule = "/^[a-zA-Z]{1}[a-zA-Z0-9.]{2,20}\@[a-zA-Z0-9]{2,}\.[a-zA-Z]{2,5}$/";
		$rule1 = "/^[0]{1}[0-9]{9,10}$/";
		if($name == "" || $email == "" || $phone == "" || $message == ""){
			echo 1;
		}else{
			if(preg_match($rule1,$phone)){
				if(preg_match($rule,$email)){
					$data = array(
					"con_name"	=> $name,
					"con_email" => $email,
					"con_phone" => $phone,
					"con_email" => $email,
					"con_full"	=> $message,
					"con_date"	=> date('Y/m/d')
				);
				sendmail($data);
				$db->insert("tbl_contact",$data);
				//echo 4;
				}else{
					echo 2;
				}
			}else{
				echo 3;
			}
		}
		die();
	}
	function sendmail($data){
			$db = new model_mdel;
		   $data['info'] = $db->getconfig(1);
		   $mesnger = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html>
                    <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                    </head>
                    <body>
                    <h2 style="font-size: 16px;">Liên hệ từ Mobilethansam.com</h2>
                    <p>THÔNG TIN LIÊN HỆ</p>

            ';
			$mesnger .= "Họ tên : ".$data['con_name']." <br />Email : ".$data['con_email']." <br />Phone : ".$data['con_phone']." <br />Nội dung : ".$data['con_full']."";
			$mesnger .= '</body></html> ';
			send_mail_helper(''.$data['info']['config_email'].'', 'Liên hệ từ Mobilethansam.com', htmlspecialchars_decode($mesnger));
	   }
	   
	$data['template'] = "main_home/contact_view";
	loadview("layout",$data);
?>